home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / StdEnv / StdList.dcl < prev    next >
Encoding:
Modula Definition  |  1997-04-02  |  4.3 KB  |  122 lines  |  [TEXT/3PRM]

  1. definition module StdList
  2.  
  3. //    ****************************************************************************************
  4. //    Concurrent Clean Standard Library Module Version 1.1
  5. //    Copyright 1995 University of Nijmegen
  6. //    ****************************************************************************************
  7.  
  8.  
  9. import StdClass
  10.  
  11. import StdInt
  12. export Eq Int
  13. export Eq Char
  14.  
  15. import StdChar
  16. export Ord Int
  17. export Ord Char
  18. export + Int
  19. export zero Int
  20. export one Int
  21.  
  22. export toChar Char
  23. export fromChar Char
  24.  
  25. //    Instances of overloaded functions:
  26.  
  27. instance ==     [a] | Eq a
  28.  
  29. instance <        [a] | Ord a
  30.  
  31. instance length        []
  32. instance %            [a]
  33.  
  34. instance toString     [x] | toChar x     // Convert [x]    via [Char] into String
  35. instance fromString [x] | fromChar x // Convert String via [Char] into [x]
  36.  
  37. //    List Operators:
  38.  
  39. (!!)     infixl 9    :: ![.a] Int -> .a                //    Get nth element of the list
  40. (++)    infixr 5    :: ![.a] u:[.a] -> u:[.a]        //    Append args
  41. flatten                :: ![[.a]] -> [.a]                //    e0 ++ e1 ++ ... ++ e##
  42. isEmpty                :: ![.a] -> Bool                //    [] ?
  43.  
  44. //    List breaking or permuting functions:
  45.  
  46. hd            :: ![.a] -> .a                            //    Head of the list
  47. tl            :: !u:[.a] -> u:[.a]                    //    Tail of the list
  48. last        :: ![.a] -> .a                            //    Last element of the list
  49. take        :: !Int [.a] -> [.a]                    //    Take first arg1 elements of the list
  50. takeWhile    :: (a -> .Bool) !.[a] -> .[a]            //    Take elements while pred holds
  51. drop        :: Int !u:[.a] -> u:[.a]                //    Drop first arg1 elements from the list
  52. dropWhile    :: (a -> .Bool) !u:[a] -> u:[a]            //    Drop elements while pred holds
  53. filter        :: (a -> .Bool) !.[a] -> .[a]            //    Drop all elements not satisfying pred
  54. insert         :: (a a -> .Bool) a !u:[a] -> u:[a]        //    Insert arg2 when pred arg2 elem holds   
  55. remove        :: !Int !u:[.a] -> u:[.a]                //    Remove arg2!arg1 from list
  56. reverse        ::![.a] -> [.a]                            //    Reverse the list
  57. span        :: (a -> .Bool) !u:[a] -> (.[a],u:[a])    //    (takeWhile list,dropWhile list)
  58. splitAt        :: !Int u:[.a] -> ([.a],u:[.a])            //    (take n list,drop n list)
  59.  
  60. //    Creating lists:
  61.  
  62. map            :: (.a -> .b) ![.a] -> [.b]                //    [f e0,f e1,f e2,...
  63. iterate        :: (a -> a) a -> .[a]                    //    [a,f a,f (f a),...
  64. indexList    :: ![.a] -> [Int]                        //    [0..maxIndex list]
  65. repeatn        :: .Int a -> .[a]                        //    [e0,e0,...,e0] of length n
  66. repeat        :: a -> [a]                                //    [e0,e0,...
  67. unzip        ::    ![(.a,.b)]         -> ([.a],[.b])        //    ([a0,a1,...],[b0,b1,...])
  68. zip2        :: ![.a] [.b]         -> [(.a,.b)]        //    [(a0,b0),(a1,b1),...
  69. zip            :: !(![.a],[.b])     -> [(.a,.b)]        //    [(a0,b0),(a1,b1),...
  70. diag2        :: !.[a] .[b]        -> [.(a,b)]            //    [(a0,b0),(a1,b0),(a0,b1),...
  71. diag3        :: !.[a] .[b] .[c]    -> [.(a,b,c)]        //    [(a0,b0,c0),(a1,b0,c0),...
  72.  
  73. //    Folding and scanning:
  74.  
  75. // for efficiency reasons, foldl and folr are defined as macros, so that
  76. // applications of these functions will be inlined
  77.  
  78. // foldl :: (.a -> .(.b -> .a)) .a ![.b] -> .a    //    op(...(op (op (op r e0) e1)...e##)
  79. foldl op r l
  80.     :==    foldl r l
  81.     where
  82.         foldl r []        = r
  83.         foldl r [a:x]    = foldl (op r a) x
  84.  
  85. // foldr :: (.a -> .(.b -> .b)) .b ![.a] -> .b    //    op e0 (op e1(...(op r e##)...)
  86. foldr op r l
  87.     :== foldr r l
  88.     where
  89.         foldr r []        = r
  90.         foldr r [a:x]    = op a (foldr r x)
  91.  
  92. scan        ::  (a -> .(.b -> a)) a ![.b] -> .[a]    //    [r,op r e0,op (op r e0) e1,...
  93.  
  94. //    On Booleans
  95.  
  96. and            :: ![.Bool] -> Bool                        //    e0 && e1 ... && e##
  97. or            :: ![.Bool] -> Bool                        //    e0 || e1 ... || e##
  98. any            :: (.a -> .Bool) ![.a] -> Bool            //    True, if ei is True for some i
  99. all            :: (.a -> .Bool) ![.a] -> Bool            //    True, if ei is True for all i
  100.  
  101. maxList        :: !.[a]         -> a         | Ord a        //    Maximum element of list
  102. minList        :: !.[a]         -> a         | Ord a        //    Minimum element of list
  103. sort        :: !.[a]         -> .[a]     | Ord a        //    Sort the list
  104. merge :: !u:[a] !u:[a] -> u:[a] | Ord a                //    Merge two sorted lists giving a sorted list
  105.  
  106. //    When equality is defined on list elements
  107.  
  108. isMember        ::    a    !.[a]    -> Bool     | Eq a    //    Is element in list
  109. removeMembers    :: u:[a] .[a]     -> u:[a]     | Eq a    //    Remove arg2s from list arg1
  110. removeDup        :: !.[a]         -> .[a]     | Eq a    //    Remove all duplicates from list
  111.  
  112. limit            :: !.[a]         -> a         | Eq a    //    [...,a,a]
  113.  
  114. //    When addition is defined on list elements
  115.  
  116. sum :: !.[a] -> a |  + , zero  a                    //    sum of list elements, sum [] = zero
  117.  
  118. //    When multiplication and addition is defined on list elements
  119.  
  120. prod :: !.[a] -> a | * , one  a                     //    product of list elements, prod [] = one
  121. avg :: !.[a] -> a | / , IncDec a                    //    average of list elements, avg [] gives error!
  122.